﻿#region License
/* ---------------------------------------------------------------- *
 *
 * This file is part of the Xcoordination Application Space
 * ("XcoAppSpace") http://www.xcoordination.com
 *
 * Copyright (C) 2009 Xcoordination http://www.xcoordination.com
 *
 * XcoAppSpace is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General
 * Public License as published by the Free Software Foundation.
 *
 * XcoAppSpace is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/
 * or write to the Free Software Foundation, Inc., 51 Franklin
 * Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * ---------------------------------------------------------------- */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using XcoAppSpaces.Contracts.Communication;
using XcoAppSpaces.Contracts.Exceptions;
using XcoAppSpaces.Contracts.Messaging;
using XcoAppSpaces.Contracts.PortWiring;
using XcoAppSpaces.Contracts.Transport;
using XcoAppSpaces.Contracts.WorkerResolve;
using XcoAppSpaces.Core;
using XcoAppSpaces.Core.Ports;
using XcoAppSpaces.Security.None;
using CausalityContext = XcoAppSpaces.Contracts.Messaging.CausalityContext;

namespace XcoAppSpaces.Serialization.Binary
{
	//TODO: create surrogates for the following types, which don't have the Serializable Attribute any more:
	//+ XcoPortSerializationInfo
	//+ XcoConcurrent, XcoExclusive Attributes
	//+ XcoMessage (the events are not serialized!)
	//+ WorkerMessage, RemotePortMessage, CausalityContext, CausalityInfo
	//+ ResolveRequest/Response
	//+ all Exceptions
	//- XcoCommunicationError
	//+ XcoBasicClientCredentials

	class SurrogateSelector : ISurrogateSelector
	{
		private readonly PortSerializationSurrogate portSurrogate;
		private readonly TypeSerializationSurrogate typeSurrogate;
		private readonly ExceptionSerializationSurrogate exceptionSurrogate;
		private readonly Type portType = typeof(IPort);
		//private readonly Type typeType = typeof(Type);

		private Dictionary<Type, ISerializationSurrogate> messageTypeSurrogates =
			new Dictionary<Type, ISerializationSurrogate>
				{
					{typeof (XcoConcurrentAttribute), new XcoConcurrentSerializationSurrogate()},
					{typeof (XcoExclusiveAttribute), new XcoExclusiveSerializationSurrogate()},
					{typeof (XcoMessage), new XcoMessageSerializationSurrogate()},
					{typeof (WorkerMessage), new WorkerMessageSerializationSurrogate()},
					{typeof (RemotePortMessage), new RemotePortMessageSerializationSurrogate()},
					{typeof (CausalityContext), new CausalityContextSerializationSurrogate()},
					{typeof (CausalityInfo), new CausalityInfoSerializationSurrogate()},
					{typeof (ResolveRequest), new ResolveRequestSerializationSurrogate()},
					{typeof (ResolveResponse), new ResolveResponseSerializationSurrogate()},
					{typeof (XcoBasicClientCredentials), new XcoClientCredentialsSerializationSurrogate()},
					{typeof (XcoCommunicationError), new XcoCommErrorSerializationSurrogate()}
				};

		public SurrogateSelector(IPortWirer portWirer)
		{
			portSurrogate = new PortSerializationSurrogate(portWirer);
			typeSurrogate = new TypeSerializationSurrogate();
			exceptionSurrogate = new ExceptionSerializationSurrogate();
		}

		public void ChainSelector(ISurrogateSelector selector)
		{
		}

		public ISurrogateSelector GetNextSelector()
		{
			return null;
		}

		public ISerializationSurrogate GetSurrogate(Type type, StreamingContext context, out ISurrogateSelector selector)
		{
			//Console.WriteLine("SurrogateSelector - Type: " + type.FullName + " " + type.BaseType.FullName);
			ISerializationSurrogate surr;
			if (messageTypeSurrogates.TryGetValue(type, out surr))
			{
				selector = this;
				return surr;
			}
			if ((type == portType || type.GetInterfaces().Contains(portType)))
			{
				selector = this;
				return portSurrogate;
			}
			if (type.FullName == "System.RuntimeType")
			{
				selector = this;
				return typeSurrogate;
			}
			if (typeof (XcoException).IsAssignableFrom(type))
			{
				selector = this;
				return exceptionSurrogate;
			}
			selector = null;
			return null;
		}
	}
}